using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class GuideSystem : MonoBehaviour
{
    public GameObject guidePanel; // 引导界面
    public Text dialogueText; // 用于显示文本
    public Button nextButton; // 下一步按钮
    public float typingSpeed = 0.05f; // 打字机速度

    public string[] dialogues =
    {
        "欢迎来到游戏！",
        "你可以使用WASD移动角色。",
        "按空格键进行跳跃。",
        "祝你游戏愉快！"
    };

    private int currentIndex = 0; // 当前显示的文本索引
    private bool isTyping = false; // 是否正在打字

    private void Start()
    {
        guidePanel.SetActive(false); // 初始隐藏
        nextButton.onClick.AddListener(OnNextButtonClick);
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            ShowGuide();
        }
    }

    // 外部调用此方法来显示引导
    public void ShowGuide()
    {
        guidePanel.SetActive(true);
        currentIndex = 0; // 重置索引
        StartCoroutine(TypeText(dialogues[currentIndex]));
    }

    private void OnNextButtonClick()
    {
        if (isTyping) return; // 正在打字时点击无效

        currentIndex++;
        if (currentIndex < dialogues.Length)
        {
            StartCoroutine(TypeText(dialogues[currentIndex])); // 显示下一个文本
        }
        else
        {
            guidePanel.SetActive(false); // 隐藏引导系统
        }
    }

    private IEnumerator TypeText(string text)
    {
        isTyping = true;
        dialogueText.text = "";
        foreach (char letter in text)
        {
            dialogueText.text += letter;
            yield return new WaitForSeconds(typingSpeed);
        }
        isTyping = false;
    }
}
